package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.contract.model.entity.ContractCategory;
import com.wing.contract.model.response.ContractCategoryVO;
import com.wing.purchase.dao.SupplierDueSubjectDao;
import com.wing.purchase.model.entity.SupplierDueSubject;
import com.wing.purchase.model.request.SupplierDueSubjectForm;
import com.wing.purchase.model.response.SupplierDueSubjectVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 尽调科目-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SupplierDueSubjectServiceImpl extends ServiceImpl<SupplierDueSubjectDao, SupplierDueSubject> implements SupplierDueSubjectService {

    @Override
    public int add(SupplierDueSubjectForm supplierDueSubjectForm) {
        //校验
        SupplierDueSubject supplierDueSubject =new SupplierDueSubject();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SupplierDueSubject.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(supplierDueSubjectForm, supplierDueSubject, copyOptions);
        if(StringUtils.isNotBlank(supplierDueSubjectForm.getTextParam())){
            supplierDueSubject.setParamValue(supplierDueSubjectForm.getTextParam());
        }
        if(StringUtils.isNotBlank(supplierDueSubjectForm.getDateParam())){
            supplierDueSubject.setParamValue(supplierDueSubjectForm.getDateParam());
        }
        if(StringUtils.isNotBlank(supplierDueSubjectForm.getTimeParam())){
            supplierDueSubject.setParamValue(supplierDueSubjectForm.getTimeParam());
        }
        return baseMapper.insert(supplierDueSubject);
    }

    @Override
    public List<SupplierDueSubjectVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SupplierDueSubjectVO> pageList4app(Page page, Map<String, Object> params) {
        List<SupplierDueSubjectVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SupplierDueSubjectVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        SupplierDueSubjectVO supplierDueSubjectVO = baseMapper.get4appById(id);
        if (supplierDueSubjectVO!=null){
            SupplierDueSubject supplierDueSubject = new SupplierDueSubject();
            supplierDueSubject.setId(id);
            supplierDueSubject.setIsDeleted(true);
            return baseMapper.updateById(supplierDueSubject);
        }
        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        SupplierDueSubjectVO supplierDueSubjectVO = baseMapper.get4appById(id);
        if (supplierDueSubjectVO!=null){
            SupplierDueSubject supplierDueSubject = new SupplierDueSubject();
            supplierDueSubject.setId(id);
            supplierDueSubject.setIsDeleted(false);
            return baseMapper.updateById(supplierDueSubject);
        }
        return 0;
    }

}
